package com.masterclient.app.modules.goods.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.bean.goods.ProductBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.masterclient.app.modules.goods.newpage.activity.NewCommodityDetailActivity;

import jp.wasabeef.glide.transformations.CropCircleWithBorderTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * 作者：sean
 * 创建时间：2017/6/23 16:32
 * 功能说明：
 */

public class GoodsRecyclerAdapter extends BaseQuickAdapter<ProductBean, BaseViewHolder> {

    private int width;

    public GoodsRecyclerAdapter() {
        super(R.layout.item_search_good);
        this.strings = new String[]{};
        width = (int)((ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(41))/2f);
    }

    private String[] strings;
    /*
     **source_page, source_module, source_element, key_word, key_word_type
     */
    public void setStrings(String... strings) {
        this.strings = strings;
    }

    @Override
    protected void convert(BaseViewHolder helper, ProductBean item) {

        ImageView imageView =  helper.getView(R.id.iv_goods);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
        layoutParams.height = width;
        layoutParams.width = width;
        ViewTransformUtil.glideImageView(mContext,item.getSh_image(),imageView, new Transformation[]{new CenterCrop(),
                        new RoundedCornersTransformation(ScreenUtils.dp2px(7), 0, RoundedCornersTransformation.CornerType.TOP)},
                R.mipmap.defaults);


        TextView buy_giveaway = helper.getView(R.id.buy_giveaway);
        buy_giveaway.setVisibility(!TextUtils.isEmpty(item.getSh_tag())?View.VISIBLE:View.GONE);
        buy_giveaway.setText(TextUtils.isEmpty(item.getSh_tag())?"":item.getSh_tag());

        helper.setGone(R.id.overseas_direct_mail_ll,!TextUtils.isEmpty(item.getSh_country()));
        helper.setGone(R.id.overseas_direct_mail_tag,!TextUtils.isEmpty(item.getSh_send_type())&&"2".equals(item.getSh_send_type()));
        helper.setText(R.id.overseas_direct_mail_name,!TextUtils.isEmpty(item.getSh_country())?item.getSh_country():"");
        ViewTransformUtil.glideImageView(mContext,item.getSh_country_logo(),helper.getView(R.id.overseas_direct_mail_icon),
                new CropCircleWithBorderTransformation(0,mContext.getResources().getColor(R.color.white)),R.mipmap.defaults);
        ((TextView)helper.getView(R.id.overseas_direct_mail_name)).setMaxEms(!TextUtils.isEmpty(item.getSh_send_type())&&"2".equals(item.getSh_send_type())?4:20);

        ImageView storeIcon = helper.getView(R.id.iv_store);
        storeIcon.setVisibility(TextUtils.isEmpty(item.getSh_shop_icon())?View.GONE:View.VISIBLE);
        ViewTransformUtil.glideImageView(mContext,item.getSh_shop_icon(),storeIcon,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);

        helper.setText(R.id.tv_sub_title,item.getSh_name());
        helper.setText(R.id.tv_title,item.getSh_subname());
        if("2".equals(item.getSh_type())){
            helper.setText(R.id.tv_price_now,item.getSh_integral_price() + "金币");
        }else{
            helper.setText(R.id.tv_price_now,item.getSh_show_price());
        }
        Utils.setDin(helper.getView(R.id.tv_price_now),mContext);
        Utils.setDin(helper.getView(R.id.tv_price_unit),mContext);
        TextView tv_bofang = helper.getView(R.id.tv_bofang);
        tv_bofang.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/iconfont.ttf"));

        ((TextView)helper.getView(R.id.tv_price_older)).setPaintFlags(Paint. STRIKE_THRU_TEXT_FLAG| Paint.ANTI_ALIAS_FLAG);
        ImageView ivActivity = helper.getView(R.id.iv_activity);

        ImageView ivTagNew = helper.getView(R.id.iv_tag_new);
        View ivShouqing = helper.getView(R.id.ll_shouqing);
        LinearLayout ll_user = helper.getView(R.id.ll_user);
        LinearLayout ll_earn = helper.getView(R.id.ll_earn);
        TextView tv_price = helper.getView(R.id.tv_price);
        TextView tv_money_earn = helper.getView(R.id.tv_money_earn);
        LinearLayout ll_double_earn = helper.getView(R.id.ll_double_earn);
        ImageView ivStore = helper.getView(R.id.iv_store);
        if (!TextUtils.isEmpty(item.getSh_shop_icon())){
            ViewTransformUtil.glideImageView(mContext,item.getSh_shop_icon(),ivStore,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
            ivStore.setVisibility(View.VISIBLE);
        }else {
            ivStore.setVisibility(View.GONE);
        }


//        Utils.setDin(tv_money_earn,mContext);
        if(TextUtils.isEmpty(item.getSh_partner_commission())||0==Double.valueOf(item.getSh_partner_commission())){
            tv_money_earn.setVisibility(View.GONE);
        }else {
            tv_money_earn.setVisibility(View.VISIBLE);
            tv_money_earn.setText("赚"+item.getSh_partner_commission());

            if (!TextUtils.isEmpty(item.getSh_double_partner_commission())&&
                    Float.parseFloat(item.getSh_double_partner_commission())>0){
                tv_money_earn.setVisibility(View.GONE);
                ll_double_earn.setVisibility(View.VISIBLE);
                helper.setText(R.id.tv_double_earn,"赚"+item.getSh_double_partner_commission());
            }else {
                ll_double_earn.setVisibility(View.GONE);
            }
        }


        if (!TextUtils.isEmpty(item.getSh_stock())) {
            if (Long.valueOf(item.getSh_stock()) <= 0) {
                ivShouqing.setVisibility(View.VISIBLE);
            } else {
                ivShouqing.setVisibility(View.GONE);
            }
        }

        if("1".equals(item.getSh_is_new())){
            ivTagNew.setVisibility(View.VISIBLE);
        }else {
            ivTagNew.setVisibility(View.GONE);
        }

        TextView newcomer = helper.getView(R.id.newcomer_price);
        if (!TextUtils.isEmpty(item.getSh_activity_flag())&&"2".equals(item.getSh_activity_flag())){
            newcomer.setVisibility(View.VISIBLE);
            newcomer.setText(item.getSh_label_name());
            helper.setText(R.id.tv_price_now,item.getSh_new_user_promotion_price());
        }else {
            newcomer.setVisibility(View.GONE);
        }

        TextView tvActivityMj = helper.getView(R.id.tv_activity_mj);
        if (!TextUtils.isEmpty(item.getSh_meet_reduce_name())){
            tvActivityMj.setVisibility(View.VISIBLE);
            tvActivityMj.setText(item.getSh_meet_reduce_name());
        }else {
            tvActivityMj.setVisibility(View.GONE);
        }

        if(item.getSh_label()!=null
                && !TextUtils.isEmpty(item.getSh_label().getSh_icon_height())
                &&!TextUtils.isEmpty(item.getSh_label().getSh_icon_width())) {
            ViewTransformUtil.glideImageView(mContext,item.getSh_label().getSh_icon(),ivActivity,R.mipmap.defaults);
            ivActivity.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivActivity.getLayoutParams();
            params.height = ScreenUtils.dp2px(Integer.valueOf(item.getSh_label().getSh_icon_height()) / 2*0.8f);
            params.width = ScreenUtils.dp2px(Integer.valueOf(item.getSh_label().getSh_icon_width()) / 2*0.8f);
            switch (item.getSh_label().getSh_location()) {
                case "left_top":
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    break;
                case "right_top":
                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    break;
                case "left_bottom":
                    params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.iv_goods);
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    params.setMargins(ScreenUtils.dp2px(7), 0, 0, ScreenUtils.dp2px(7));
                    break;
                case "right_bottom":
                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.iv_goods);
                    params.setMargins(0, 0, 0,0);
                    break;
            }
            ivActivity.setLayoutParams(params);
        }else {
            ivActivity.setVisibility(View.GONE);
        }

        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, NewCommodityDetailActivity.class);
                intent.putExtra("id",item.getSh_id());
                intent.putExtra("source_page", strings.length>0?strings[0]:"");
                intent.putExtra("source_module",strings.length>1?strings[1]:"");
                intent.putExtra("source_element",strings.length>2?strings[2]:"");
                intent.putExtra("key_word", strings.length>3?strings[3]:"");
                intent.putExtra("key_word_type", strings.length>4?strings[4]:"");
                intent.putExtra("source_position",helper.getAdapterPosition()-getHeaderLayoutCount()+"");
                mContext.startActivity(intent);
            }
        });
    }
}
